/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.system.application.facades.facesmessages;

import br.cesupa.fabsoft.nomedoprojeto.system.application.facades.*;
import javax.faces.application.FacesMessage;

/**
 * Manages translations for the messages used on validation.
 *
 * @author Breno Leite 
 */
public class FacesMessageTranslationContext {

    private FacesMessageFacade facesMessageFacade;
    private FacesMessage processedMessage;

    public FacesMessageTranslationContext(final FacesMessageFacade facesMessageFacade) {
        this.facesMessageFacade = facesMessageFacade;
    }

    /**
     * Uses the given message as the key to a translated resource stored in the
     * <code>faces-validation.properties</code> resource bundle.
     */
    public FacesMessageParameterContext translated() {
        this.processedMessage = new FacesMessage(getFacesMessageFacade().getSeverity(),
                translateMessage(getFacesMessageFacade().getMessage()),
                translateMessageDetails(getFacesMessageFacade().getMessage()));
        return new FacesMessageParameterContext(this);
    }

    /**
     * Uses no resource bundle. The given message is the text that will be
     * showed in the view.
     */
    public FacesMessageParameterContext withoutTranslation() {
        this.processedMessage = new FacesMessage(getFacesMessageFacade().getSeverity(),
                getFacesMessageFacade().getMessage(), getFacesMessageFacade().getMessage());
        return new FacesMessageParameterContext(this);
    }

    /**
     * @see TranslationFacade#translateFacesMessage(java.lang.String)
     */
    private String translateMessage(final String message) {
        return ApplicationFacade.getTranslationFacade().translateFacesMessage(message);
    }

    /**
     * @see TranslationFacade#translateFacesMessage(java.lang.String)
     */
    private String translateMessageDetails(final String message) {
        String translatedMessage = ApplicationFacade.getTranslationFacade().
                translateFacesMessage(message + ".DETAILS");

        if (translatedMessage.equals(message + ".DETAILS")) {
            return null;
        } else {
            return translatedMessage;
        }
    }

    /**
     * @return the processedMessage
     */
    public FacesMessage getProcessedMessage() {
        return processedMessage;
    }

    /**
     * @return the facesMessageFacade
     */
    public FacesMessageFacade getFacesMessageFacade() {
        return facesMessageFacade;
    }
}
